package server.utils;

import server.main.server_main;
import model.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.AbstractMap;

import com.google.gson.Gson;

import java.util.*;

public class MessageForward implements Runnable {
    private DataInputStream dataInputStream;
    private DataOutputStream dataOutputStream;
    private int userID;
    private boolean isClose;

    public MessageForward(DataInputStream input,DataOutputStream output,int userid ) {        
            this.dataInputStream = input;
            this.dataOutputStream = output;
            this.isClose = false;
            this.userID = userid;
            System.out.println("user :"+userID+"成功连接");
    }
    public int getUserID() {
        return this.userID;
    }
    public class Pair<K, V> extends AbstractMap.SimpleEntry<K, V> {
        public Pair(K key, V value) {
            super(key, value);
        }
    }
    private Pair<Integer, String> receive() {
        String msg = "";
        Integer roomID = -1;
        Pair<Integer, String> pair = null;
        Message mes = new Message();
        Gson gson = new Gson();
         
        try {
        	String json = dataInputStream.readUTF();
            mes = gson.fromJson(json, Message.class);
            
            if(mes.getType() == MsgType.COMMON_MESSAGE) {
            	msg = mes.getText();
            	roomID = mes.getRoomID();
            	pair = new Pair<Integer,String>(roomID, msg);
            }
        } catch (IOException e) {
            this.isClose = true;
            System.out.println(e);
            try {
                this.dataInputStream.close();
                this.dataOutputStream.close();
                server_main.connSocketList.remove(this);
                System.out.println(e);
            } catch (IOException ce) {
                System.out.println(ce);
            }
        }
        return pair;
    }
    private void send(String msg) {
        try {
            this.dataOutputStream.writeUTF(msg);
        } catch (IOException e) {
            this.isClose = true;
            System.out.println(e);
            try {
                this.dataInputStream.close();
                this.dataOutputStream.close();
                server_main.connSocketList.remove(this);
                System.out.println(e);        
            } catch (IOException ce) {
                System.out.println(ce);
            }
        }
    }
    private void forward() {
        Pair<Integer, String> pair = this.receive();
        DAO.RoomDao rommdao = new DAO.RoomDao(server_main.db.con);
        DAO.TotalDao Tdao = new DAO.TotalDao(server_main.db.con);
        String msg = pair.getValue();
        int roomID = pair.getKey();
        List<MessageForward> list  = server_main.connSocketList;
        
        Tdao.insertMessage("test", userID, msg);
        Vector<Integer> users =rommdao.getAllUser(roomID);
        System.out.println("mes:"+msg);
        System.out.println("users size:"+users.size());
        for(MessageForward l : list) {
        	for(int i = 0;i < users.size();i++) {
        		if(l.userID == this.userID) {
            		continue;
            	}
        		
        		if(l.userID == users.elementAt(i)) {
        			l.send(msg);
        			System.out.println("鎴愬姛鍙戦��");
        		}
        	}
        	
        }
        
    }
    
    public void run() {
        while(!this.isClose) {
            this.forward();
        }
    }
    
 
}
